package com.lee.carinforecord;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class InfoWindow {
    public static WindowManager.LayoutParams wmParams = null;
    public static WindowManager mWindowManager;
    public static LinearLayout mainBox;
    public static TextView remainingTextView, milTextView, oilTextView;

    public static void show(Context mContext) {
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        if (mainBox == null) {
            mainBox = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.info_window,null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(420, 54);
            mainBox.setLayoutParams(params);
            remainingTextView = (TextView) mainBox.findViewById(R.id.remaining_mils);
            remainingTextView.setText("--");

            milTextView = (TextView) mainBox.findViewById(R.id.current_mils);
            milTextView.setText("--");

            oilTextView = (TextView) mainBox.findViewById(R.id.current_oil);
            oilTextView.setText("--");

            wmParams = new WindowManager.LayoutParams();
            wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            wmParams.format = PixelFormat.RGBA_8888;
            // 设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
            wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            wmParams.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
            wmParams.x = 16;
            wmParams.y = 0;

            // 设置悬浮窗口长宽数据
            wmParams.width = 420;
            wmParams.height = 54;

            mWindowManager.addView(mainBox, wmParams);

            mainBox.setOnTouchListener(new FloatingOnTouchListener());
            TextView closeBtn = (TextView) mainBox.findViewById(R.id.close);
            closeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    InfoWindow.hide();
                }
            });
        }

    }

    public static void update(String remaining, String mil, String oil) {
        remainingTextView.setText(remaining);
        milTextView.setText(mil);
        oilTextView.setText(oil);
        Float remainingVal = Float.parseFloat(remaining);
        if(remainingVal < 100){
            remainingTextView.setTextColor(0xFFF56C6C);
        }else if(remainingVal < 140 ){
            remainingTextView.setTextColor(0xFFE6A23C);
        }
    }

    public static void hide() {
        try {
            if (mWindowManager != null){
                mWindowManager.removeView(mainBox);
                mWindowManager=null;
                mainBox=null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class FloatingOnTouchListener implements View.OnTouchListener {
        private int x;
        private int y;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    wmParams.x = wmParams.x + movedX;
                    wmParams.y = wmParams.y + movedY;
                    mWindowManager.updateViewLayout(view, wmParams);
                    break;
                default:
                    break;
            }
            return false;
        }
    }
}
